﻿#include "buybtnform.h"
#include "ui_buybtnform.h"

buyBtnForm::buyBtnForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::buyBtnForm)
{
    ui->setupUi(this);
    count = 0;
    ui->label->setText(QString::number(count));
    ui->addBtn->setStyleSheet("QPushButton#addBtn{border-image: url(:/pic/加.png);}"
                                 "QPushButton#addBtn:hover{border-image: url(:/pic/加2.png);}"
                                 "QPushButton#addBtn:pressed{border-image: url(:/pic/加3.png);}");
    ui->subtractBtn->setStyleSheet("QPushButton#subtractBtn{border-image: url(:/pic/减.png);}"
                                 "QPushButton#subtractBtn:hover{border-image: url(:/pic/减2.png);}"
                                 "QPushButton#subtractBtn:pressed{border-image: url(:/pic/减3.png);}");
    if(count == 0)
        ui->subtractBtn->setEnabled(false);

}

buyBtnForm::~buyBtnForm()
{
    delete ui;
}

/*
 *  商品购买
*/
void buyBtnForm::getRowBuyAdd(const int &index, const int &row, const int &inventory)
{
    this->index = index;
    this->rowAdd = row;
    this->inventory = inventory;
}

void buyBtnForm::getRowBuySub(const int &index, const int &row, const int &inventory)
{
    this->index = index;
    this->rowSub = row;
    this->inventory = inventory;
}

/*
 *  购物车
*/
void buyBtnForm::getRowCartBuyAdd(const int &index, const int &row, const int &inventory)
{
    this->index = index;
    this->rowAddCart = row;
    this->inventoryCart = inventory;
    this->countCart = inventory;
    ui->label->setText(QString::number(countCart));
    ui->addBtn->setEnabled(false);
    ui->subtractBtn->setEnabled(true);

}

void buyBtnForm::getRowCartBuySub(const int &index, const int &row, const int &inventory)
{
    this->index = index;
    this->rowSubCart = row;
    this->inventoryCart = inventory;
    this->countCart = inventory;
    ui->label->setText(QString::number(countCart));
    ui->addBtn->setEnabled(false);
    ui->subtractBtn->setEnabled(true);
}



void buyBtnForm::on_subtractBtn_clicked()
{
    if(this->index == 0)
    {
        if(count == 0)
        {
            ui->subtractBtn->setEnabled(false);
        }
        else
        {
            ui->addBtn->setEnabled(true);
            count--;
            ui->label->setText(QString::number(count));
            emit sendRowSub(rowSub, count);
        }
    }
    else if(this->index == 1)
    {
        if(countCart == 0)
        {
            ui->subtractBtn->setEnabled(false);
        }
        else
        {
            ui->addBtn->setEnabled(true);
            countCart--;
            ui->label->setText(QString::number(countCart));
            emit sendRowSubCart(rowSubCart, countCart);
        }
    }


}

void buyBtnForm::on_addBtn_clicked()
{
    if(this->index == 0)
    {
        if(count == inventory)
        {
            ui->addBtn->setEnabled(false);
        }
        else
        {
            ui->subtractBtn->setEnabled(true);
            count++;
            ui->label->setText(QString::number(count));
            emit sendRowAdd(rowAdd, count);
        }
    }
    else if(this->index == 1)
    {
//        ui->addBtn->hide();
        if(countCart == inventoryCart)
        {
            ui->addBtn->setEnabled(false);
        }
        else
        {
            ui->subtractBtn->setEnabled(true);
            countCart++;
            ui->label->setText(QString::number(countCart));
            emit sendRowAddCart(rowAddCart, countCart);
        }
    }




}
